import 'package:elephant_fish/http/core/hi_net.dart';
import 'package:elephant_fish/http/request/base_request.dart';
import 'package:elephant_fish/http/request/safes_set_password_request.dart';
import 'package:elephant_fish/util/TxUtils.dart';
import 'package:elephant_fish/util/size_util.dart';
import 'package:elephant_fish/util/string_util.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';

import 'login_page.dart';

class SafesSetPasswordPage extends StatefulWidget {
  const SafesSetPasswordPage({Key key}) : super(key: key);

  @override
  _SafesSetPasswordPageState createState() => _SafesSetPasswordPageState();
}

class _SafesSetPasswordPageState extends State<SafesSetPasswordPage> {
  bool setPassword = false;
  bool sample = false;
  TextEditingController passController = TextEditingController();
  TextEditingController rePassController = TextEditingController();
  String password;
  String rePass;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Color(0xff140c3d),
      appBar: AppBar(
        backgroundColor: Color(0xff140c3d),
        centerTitle: true,
        leading: GestureDetector(
          child: Icon(
            Icons.arrow_back_ios,
            size: SizeUtil.px(32),
            color: Color(0xfff3f3f3),
          ),
          onTap: () {
            Navigator.pop(context);
          },
        ),
        title: Text(
          '设置二级密码',
          style: TextStyle(fontSize: SizeUtil.px(32), color: Color(0xfff5f5f5)),
        ),
      ),
      body: Column(
        children: [
          SizedBox(
            height: SizeUtil.px(90),
          ),
          Align(
            alignment: Alignment.topLeft,
            child: Padding(
              padding: EdgeInsets.only(left: SizeUtil.px(60)),
              child: Text(
                '您还没有设置密码',
                style: TextStyle(
                    fontSize: SizeUtil.px(32), color: Color(0x50ffffff)),
              ),
            ),
          ),
          Align(
              alignment: Alignment.topLeft,
              child: Padding(
                padding: EdgeInsets.only(left: SizeUtil.px(60)),
                child: Text(
                  '为了您的资金安全，请先设置支付密码！',
                  style: TextStyle(
                      fontSize: SizeUtil.px(32), color: Color(0x50ffffff)),
                ),
              )),
          SizedBox(
            height: SizeUtil.px(63),
          ),
          Container(
            width: SizeUtil.px(630),
            child: TextField(
              controller: passController,
              textInputAction: TextInputAction.done,
              keyboardType: TextInputType.number,
              obscureText: true,
              style: TextStyle(
                  fontSize: SizeUtil.px(28), color: Color(0xffffffff)),
              decoration: InputDecoration(
                  hintText: '请设置密码',
                  hintStyle: TextStyle(color: Colors.white),
                  counterText: '',
                  enabledBorder: new UnderlineInputBorder(
                    // 不是焦点的时候颜色
                    borderSide: BorderSide(color: Color(0xffffffff)),
                  ),
                  focusedBorder: new UnderlineInputBorder(
                    // 焦点集中的时候颜色
                    borderSide: BorderSide(color: Color(0xffffffff)),
                  )),
              maxLength: 6,
              onChanged: (text) {
                password = text;
                checkInput();
              },
            ),
          ),
          SizedBox(height: SizeUtil.px(52),),
          Container(
            width: SizeUtil.px(630),
            child: TextField(
              controller: rePassController,
              textInputAction: TextInputAction.done,
              keyboardType: TextInputType.number,
              obscureText: true,
              style: TextStyle(
                  fontSize: SizeUtil.px(28), color: Color(0xffffffff)),
              decoration: InputDecoration(
                  hintText: '请再次输入密码',
                  hintStyle: TextStyle(color: Colors.white),
                  counterText: '',
                  enabledBorder: new UnderlineInputBorder(
                    // 不是焦点的时候颜色
                    borderSide: BorderSide(color: Color(0xffffffff)),
                  ),
                  focusedBorder: new UnderlineInputBorder(
                    // 焦点集中的时候颜色
                    borderSide: BorderSide(color: Color(0xffffffff)),
                  )),
              maxLength: 6,
              onChanged: (text) {
                rePass = text;
                checkInput();
                checkSample();
              },
            ),
          ),
          SizedBox(height: SizeUtil.px(79),),
          Align(
            alignment: Alignment.topCenter,
            child: InkWell(
              child: Container(
                width: SizeUtil.px(690),
                height: SizeUtil.px(92),
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.all(Radius.circular(SizeUtil.px(46))),
                  color: Color(0xffe62d6c),
                ),
                child: Center(
                  child: Text(
                    '提交',
                    style: TextStyle(
                        fontSize: SizeUtil.px(34), color: Color(0xffffffff)),
                  ),
                ),
              ),
              onTap: (){
               if(setPassword){
                 if(sample){
                   setPass();
                 }else{
                   TxUtils.showToast('两次输入的密码不一致', context);
                 }
               }else{
                 TxUtils.showToast('密码不能为空', context);
               }
              },
            )
          )
        ],
      ),
    );
  }
  void checkInput() {
    bool enable;
    if (isNotEmpty(rePass) && isNotEmpty(password)) {
      enable = true;
    } else {
      enable = false;
    }
    setState(() {
      setPassword = enable;
    });
  }


  void checkSample() {
    bool enable;
    if(Comparable.compare(password, rePass) == 0){
      enable = true;
    }else{
      enable = false;
    }
    setState(() {
      sample = enable;
    });
  }

  Future<void> setPass() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    String appToken = prefs.getString('app-token');
    BaseRequest request = SafesSetPasswordRequest();
    request.add('password', password).addHeader('usertoken', appToken);
    var result = await HiNet.getInstance().fire(request);
    if (result['code'] == -1) {
      TxUtils.showErrorToast('登录失效，请重新登录', context);
      Navigator.pop(context);
      Navigator.of(context).push(MaterialPageRoute(
        builder: (context) {
          return LoginPage(arguments: {'loginCode': '-1'},);
        },
      ));
    }else if(result['code'] == 0){
      TxUtils.showToast(result['data'], context);
      Navigator.pop(context);
    }else{
      TxUtils.showToast(result['message'], context);
    }
  }
}
